//
//  BKXComposeToolbar.swift
//  微博
//
//  Created by 田凯 on 16/4/24.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit
//底部toolbar 上按钮的枚举值
enum ComposeToolbarType: Int{
    case picture = 1001
    case mention = 1002
    case trend = 1003
    case emoticon = 1004
    case add = 10005


}



class BKXComposeToolbar: UIView {
    
    ///是否 是表情键盘
    var isEmotion: Bool = false{
    
        didSet{
            if isEmotion{
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        
        }
        
        
    }
    
    
    
    var emoticonButton: UIButton?
    
    
    //闭包 按钮点击
    var buttonCloseBag:((type: ComposeToolbarType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
       required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI(){
        
        self.backgroundColor = UIColor.redColor()
       let pictureButton =  addButtonWith("compose_toolbar_picture",type: .picture)
       let mentionButton =  addButtonWith("compose_mentionbutton_background",type: .mention)
       let trendButton = addButtonWith("compose_trendbutton_background",type: .trend)
        emoticonButton = addButtonWith("compose_emoticonbutton_background",type: .emoticon)
        let addButton = addButtonWith("compose_add_background",type: .add)
        
        //设置约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.bottom.top.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.bottom.top.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.bottom.top.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
            
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    
    }
    // MARK: - 按钮的点击事件 方法
    @objc func buttonClick(button: UIButton ){
    
        buttonCloseBag?(type:ComposeToolbarType(rawValue: button.tag)!)
    
    }
    
    
    // MARK: - 添加按钮的公共方法
    func addButtonWith(imagaName: String, type: ComposeToolbarType) -> UIButton{
    let button = UIButton()
        //设置tag 值
        button.tag = type.rawValue
        //添加按钮的点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
    button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
    button.setImage(UIImage(named:imagaName ), forState: UIControlState.Normal)
    button.setImage(UIImage(named: "\(imagaName)_highlighted"), forState: UIControlState.Highlighted)
        //添加到
    addSubview(button)
    return button
    }
    
}















